﻿var Icon = "#fUI/GuildMark/Mark/Animal/00002006/16#";

var txt;
var status = 0;
var minLevel = 150;           //最低等级限制
var maxLevel = 255;          //最高等级限制
var minPartySize = 1;       //最低进入限制
var maxPartySize = 6;      //最高进入限制
var maxPlay = 6;
function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {

    if (cm.getEventCount("牛魔王") == 0) {
        var Ount = 0;
    } else {
        var Ount = cm.getEventCount("牛魔王")
    }

    if (mode == -1) {
        cm.dispose();
    } else {
        if (mode == 0 && status == 0) {
            cm.dispose();
            return;
        }
        if (mode == 1)
            status++;
        else
            status--;
        if (status == 0) {
            if (cm.getPlayer().getClient().getChannel() != 3) {
                cm.sendOk("挑战 牛魔王 请到3频道!");
                cm.dispose();
                return;
            }
            var txt = Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + "\r\n\r\n";
            txt += "#r　红色预警　  #b※ 牛魔王 ※ 出世 各位勇士征战吧#k\r\n";
            txt += "#r　困难模式　[ 限制时间 120 分钟 　]　#r※ 期待光临 ※#k\r\n";
            txt += "#b　普通模式　[ 限制时间  80 分钟 　]　#r※ 欢迎光临 ※#k\r\n\r\n";
          //  txt += "　　　　　　　　　#L0##r奖品图鉴#l#k\r\n";
            txt += "#L1##b普通模式　[　2 - 6 ]　开始征战　　#d已征战 [ #r" + Ount + "#d ]#l#k\r\n\r\n\r\n";
            //txt += "#L2##r困难模式　[　2 - 6 ]　开始征战　　#d已征战 [ #r" + Ount + "#d ]#l#k\r\n\r\n\r\n";
            txt += Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + Icon + "\r\n";
            cm.sendSimpleS(txt, 2);
        } else if (status == 1) {
            if (selection == 99) {
                cm.warp(910000000);
                cm.dispose();
            } else if (selection == 1) {
                if (cm.getBossLog("牛魔王") >= maxPlay) {
                    cm.sendOk("今天你已经参与了" + maxPlay + "次，不能再参与该副本了！请明天赶早~");
                    cm.dispose();
                    return;
                }

                if (cm.getParty() == null) { // 没有组队
                    cm.sendOk("请组队后和我谈话。");
                    cm.dispose();
                } else if (!cm.isLeader()) { // 不是队长
                    cm.sendOk("请叫队长和我谈话。");
                    cm.dispose();
                } else {
                    var party = cm.getParty().getMembers();
                    var mapId = cm.getPlayer().getMapId();
                    var name = cm.getPlayer().getName();
                    var next = true;
                    var levelValid = 0;
                    var inMap = 0;
                    var it = party.iterator();
                    var idx = Array();
                    while (it.hasNext()) {
                        var cPlayer = it.next();
                        if ((cPlayer.getLevel() >= minLevel) && (cPlayer.getLevel() <= maxLevel)) {
                            levelValid += 1;
                        } else {
                            next = false;
                        }
                        if (cPlayer.getMapid() == mapId) {
                            inMap += 1;
                        }
                        idx.push(cPlayer.getId());
                    }
                    if (getBossLog(idx) >= maxPlay) {
                        cm.sendOk("队伍中有玩家已经参与过该副本" + maxPlay + "次，无法再进入，请踢出该玩家。\r\n\r\n没有次数的玩家 [ #r" + cPlayer.getName() + " #k]");
                        cm.dispose();
                        return;
                    }
                    if (party.size() < minPartySize || party.size() > maxPartySize || inMap < minPartySize) {
                        next = false;
                    }
                    if (next) {
                        var em = cm.getEventManager("NmEvent");
                        //cm.worldMessage(cm.getChar().getName() + "   带领他的队伍进入了玩具城101副本挑战任务.想去的快去组织队伍吧！");
                        if (em == null) {
                            cm.sendOk("目前正在整理该副本。等待开放。。");
                        } else {
                            if (cm.getPlayerCount(105100300) == 0 && cm.getPlayerCount(105100301) == 0) {
                                em.startInstance(cm.getParty(), cm.getMap());
                                cm.setPartyBossLog("牛魔王");
                                cm.dispose();
                                return;
                            } else {
                                cm.sendOk("目前该频道已经有人在挑战，请换个频道重新进入");
                                cm.dispose();
                            }
                        }
                        cm.dispose();
                    } else {
                        cm.sendOk("请确认你的组队员：\r\n\r\n#b1、组队员必须要" + minPartySize + "人以上，" + maxPartySize + "人以下。\r\n2、组队员等级必须要在" + minLevel + "级以上。\r\n\r\n（#r如果仍然错误, 重新下线,再登陆 或者请重新组队。#k#b）");
                        cm.dispose();
                    }
                } //判断组队
            }
        }
        /*  } else if (status == 2) {
         if (cm.getPlayer().getCSPoints(1) >= 50000) {
         cm.gainNX(1,-50000);
         cm.resetBossLog("森兰丸");
         cm.sendOk("重置成功！");
         cm.dispose();
         } else {
         cm.sendOk("你的点卷不够，无法重置。");
         cm.dispose();
         }*/
    }
}
function getBossLog(idx) {
    var idStr = "";
    for (var key in idx) {
        if (key == 0)
            idStr += idx[key];
        else
            idStr += "," + idx[key];
    }
    var sql = "SELECT max(count) as maxcount FROM bosslog where bossid = '牛魔王' and characterid in (" + idStr + ") and to_days(time) = to_days(now());";
    //java.lang.System.out.println(sql);
    var conn = cm.getConnection();
    var pstmt = conn.prepareStatement(sql);
    bosslogSql = pstmt.executeQuery();
    if (bosslogSql.next()) {
        return bosslogSql.getString("maxcount") * 1;
    }
    bosslogSql.close();
    pstmt.close();
    //conn.close();
    return 0;
}
